package com.programming.forum.controller;

import com.programming.forum.common.AppResult;
import com.programming.forum.common.ResultCode;
import com.programming.forum.config.AppConfig;
import com.programming.forum.exception.ApplicationException;
import com.programming.forum.model.Article;
import com.programming.forum.model.ArticleReply;
import com.programming.forum.model.User;
import com.programming.forum.services.IArticleReplyService;
import com.programming.forum.services.IArticleService;
import com.sun.istack.internal.NotNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22479
 * Date: 2023-11-12
 * Time: 19:33
 */
@Slf4j
@Api(tags = "回复相关接口")
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource
    private IArticleReplyService articleReplyService;
    @Resource
    private IArticleService articleService;

    @ApiOperation("回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("帖子Id") @RequestParam("id") @NotNull Long id,
                            @ApiParam("正文") @RequestParam("content") @NotNull String content) {

        //获取用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //校验用户状态
        if(user.getState() == 1) {
            //用户已禁言 FAILED_USER_BANNED      (1103,"您已被禁言, 请联系管理员, 并重新登录."),
            return AppResult.failed(ResultCode.FAILED_USER_BANNED );
        }

        //获取帖子信息
        //查询帖子详情
        Article article = articleService.selectById(id);
        if(article == null || article.getDeleteState() == 1) {
            //帖子不存在
            // 打印日志  FAILED_NOT_EXISTS        (1005, "资源不存在"),
            log.warn(ResultCode. FAILED_NOT_EXISTS .toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode. FAILED_NOT_EXISTS ));
        }

        //校验帖子状态 --是否结帖
        if(article.getState() == 1) {
            // FAILED_Article_BANNED      (1302,"帖子状态异常"),
            return AppResult.failed(ResultCode.FAILED_Article_BANNED);
        }

        //构建回复对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(id);
        articleReply.setContent(content);
        articleReply.setPostUserId(user.getId());
        //调用sql语句
        articleReplyService.create(articleReply);
        return AppResult.success();
    }


    @ApiOperation("回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@ApiParam("帖子Id")
                                                                   @RequestParam("articleId") @NotNull Long articleId) {

        //查询帖子详情
        Article article = articleService.selectById(articleId);
        //校验帖子是否存在
        if(article == null) {
            //FAILED_NOT_EXISTS        (1005, "资源不存在"),
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS );
        }
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        return AppResult.success(articleReplies);

    }
}
